package zheezes.protocol.impl.gd.converter;

import java.util.Calendar;

import zheezes.util.ByteUtil;
import zheezes.util.IntUtil;


public class DateConverter {
	public static final Calendar btov(byte[] buf) {
		return btov(buf, 0);
	}
	
	public static final Calendar btov(byte[] buf, int offset) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, IntUtil.cutLow(cal.get(Calendar.YEAR), 2)
				+ ByteUtil.bcdToBin(buf[offset]));
		cal.set(Calendar.MONTH, ByteUtil.bcdToBin(buf[offset + 1]) - 1);
		cal.set(Calendar.DATE, ByteUtil.bcdToBin(buf[offset + 2]));
		return cal;
	}

	public static final byte[] vtob(Calendar cal) {
		byte[] buf = new byte[3];
		vtob(buf, 0, cal);
		return buf;
	}
	
	public static final int vtob(byte[] buf, int offset, Calendar cal) {
		buf[offset] = ByteUtil.binToBcd((byte) (cal.get(Calendar.YEAR) % 100));
		buf[offset + 1] = ByteUtil.binToBcd((byte) (cal.get(Calendar.MONTH) + 1));
		buf[offset + 2] = ByteUtil.binToBcd((byte) (cal.get(Calendar.DATE)));
		return 3;
	}
}
